#include <game.h>

#include <math.h>

localItem::localItem()
{
	
}

localItem::~localItem()
{
	
}

int localItem::init(int i,stringw t,stringc p)
{
	id = i;
	text = new stringw(t);
	path = new stringc(p);
	return 1;
}
	
int localItem::getId()
{
	return id;
}


stringw localItem::getText()
{
	return *text;
}


stringc localItem::getPath()
{
	return *path;
}

//----------------------------------------------------------------------------------------------------

local::local() : base("local")
{
	observationMesh = new string<localItem*>;
	observationItem = new string<localItem*>;
	combinaison = new string<localItem*>;
}

local::~local()
{
	delete observationMesh;
	delete observationItem;
	delete combinaison;
}

int local::init(int i,coeur* c,IXMLReader* xmlLocal)
{
	base::init(i,c);
	log(stringc("Local xml : ") + i + " loading ...");
	while(xmlLocal && xmlLocal->read())
	{
		if (xmlLocal->getNodeType()==EXN_ELEMENT)
		{
			if (core::stringw("observationMesh") == xmlLocal->getNodeName())
			{
				addObservationMesh(xmlLocal->getAttributeValueAsInt(L"id"),xmlLocal->getAttributeValue(L"text"),xmlLocal->getAttributeValue(L"file"));
				log( stringc("New observationMesh text : id:") + xmlLocal->getAttributeValueAsInt(L"id") + ", text: " + xmlLocal->getAttributeValue(L"text") + ", sound file: " + xmlLocal->getAttributeValue(L"file"));
			}
			else if (core::stringw("observationItem") == xmlLocal->getNodeName())
			{
				addObservationItem(xmlLocal->getAttributeValueAsInt(L"id"),xmlLocal->getAttributeValue(L"text"),xmlLocal->getAttributeValue(L"file"));
				log( stringc("New observationItem text : id:") + xmlLocal->getAttributeValueAsInt(L"id") + ", text: " + xmlLocal->getAttributeValue(L"text") + ", sound file: " + xmlLocal->getAttributeValue(L"file"));
			}
			else if (core::stringw("combinaison") == xmlLocal->getNodeName())
			{
				addCombinaison(xmlLocal->getAttributeValueAsInt(L"id"),xmlLocal->getAttributeValue(L"text"),xmlLocal->getAttributeValue(L"file"));
				log( stringc("New combinaisonItem text : id:") + xmlLocal->getAttributeValueAsInt(L"id") + ", text: " + xmlLocal->getAttributeValue(L"text") + ", sound file: " + xmlLocal->getAttributeValue(L"file"));
			}
			else if (core::stringw("caption") == xmlLocal->getNodeName())
			{
				caption = xmlLocal->getAttributeValue(L"text");
				log( stringc("New caption text : ") + xmlLocal->getAttributeValue(L"text"));
			}
		}
	}


	return 1;
}

int local::update()
{
	return 1;
}

int local::close()
{
	log("Closing each observationMesh text");
	while (observationMesh->size()>0)
	{
		log(stringc("Deleting observationMesh text") + (*observationMesh)[0]->getId());
		delete (*observationMesh)[0];
		observationMesh->erase(0);
	}
	log("Closing each observationItem text");
	while (observationItem->size()>0)
	{
		log(stringc("Deleting observationItem text") + (*observationItem)[0]->getId());
		delete (*observationItem)[0];
		observationItem->erase(0);
	}
	log("Closing each combinaison text");
	while (combinaison->size()>0)
	{
		log(stringc("Deleting combinaison text") + (*combinaison)[0]->getId());
		delete (*combinaison)[0];
		combinaison->erase(0);
	}
	base::close();
	return 1;
}

int local::addObservationMesh(int id,stringw text,stringc path)
{
	localItem* l = new localItem();
	l->init(id<<8,text,path);
	(*observationMesh) += l;
	log("observationMesh text added");
	return 1;
}

int local::addObservationItem(int id,stringw text,stringc path)
{
	localItem* l = new localItem();
	l->init(id,text,path);
	(*observationItem) += l;
	log("observationItem text added");
	return 1;
}

int local::addCombinaison(int id,stringw text,stringc path)
{
	localItem* l = new localItem();
	l->init(id,text,path);
	(*combinaison) += l;
	log("combinaison text added");
	return 1;
}

localItem* local::getCombinaison()
{
	return (*combinaison)[rand()%combinaison->size()];
}

stringw local::getCaption()
{
	return caption;
}

bool local::OnEvent(const SEvent& event)
{
	return false;
}

localItem* local::getObservationMesh(int id)
{
	for (unsigned int i=0;i<observationMesh->size();i++)
		if ((*observationMesh)[i]->getId() == id)
		{
			return (*observationMesh)[i];
		}
	return 0;
}

localItem* local::getObservationItem(int id)
{
	for (unsigned int i=0;i<observationItem->size();i++)
		if ((*observationItem)[i]->getId() == id)
		{
			return (*observationItem)[i];
		}
	return 0;
}

